﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;

namespace AMM.DataAccess.Config {
    public class RoleRepository : BaseRepository<Role> {
        public Role FetchRoleByRoleId(int roleId) {
            using (var ctx = this.Context) {
                return ctx.Roles.Include("UserRoles").Include("RoleFunctions").FirstOrDefault(x => x.RoleId == roleId);
            }
        }

        public List<Role> FetchRoleList() {
            using (var ctx = this.Context) {
                return ctx.Roles.ToList();
            }
        }

        public List<Role> FetchRoleByUser(int userId) {
            using (var ctx = this.Context) {
                var query = from a in ctx.UserRoles
                            join b in ctx.Roles on a.RoleId equals b.RoleId
                            where a.UserId == userId
                            select b;

                return query.ToList();
            }
        }

        public override void Update(Role t) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.UserRoles.Where(x => x.RoleId == t.RoleId).ToList().ForEach(x => {
                            ctx.UserRoles.Remove(x);
                        });
                        ctx.RoleFunctions.Where(x => x.RoleId == t.RoleId).ToList().ForEach(x => {
                            ctx.RoleFunctions.Remove(x);
                        });
                        ctx.SaveChanges();
                        base.Update(t);
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public void DeleteMultipleRole(params int[] roleIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.UserRoles.Where(x => roleIds.Contains(x.RoleId)).ToList().ForEach(x => {
                            ctx.UserRoles.Remove(x);
                        });
                        ctx.RoleFunctions.Where(x => roleIds.Contains(x.RoleId)).ToList().ForEach(x => {
                            ctx.RoleFunctions.Remove(x);
                        });
                        ctx.Roles.Where(x => roleIds.Contains(x.RoleId)).ToList().ForEach(x => {
                            ctx.Roles.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

    }
}
